Below is a program that checks whether a number entered by the user is even or odd. An Even number is a number that can be divided by the number 2 and have a remainder of zero. An odd number is a number that can be divided by 2 but with a remainder of 1.

To better understand this program, you need to be familiar with the following C# topic:

Check Even / Odd number using C#

    class Program
    {
        static void Main(string[] args)
        {
            int number;
            Console.Write("Please enter a number : ");
            number = int.Parse(Console.ReadLine());
            if (number % 2 == 0)
            {
                Console.Write("The number '" + number + "' is Even");
                Console.Read();
            }
            else
            {
                Console.Write("The number '" + number + "' is Odd");
                Console.Read();
            }
        }
    }

Output

Please enter a number: 12
The number ’12’ is Even

Please enter a number: 19
The number ’19’ is Odd

Code explanation

The program above take a number from the user, then check the modulus of that number if divided by 2. If the modulus is zero, then it’s an Even number. Else, it’s an Odd Number.

The Modulus, also known as modulo or mod, is represented in the code by the operator %

Related articles

Last modified: March 27, 2019